// B站查看视频集的学习进度
if (!location.href.startsWith('https://www.bilibili.com/')) {
  throw '请打开B站视频教程'
}

console.log('开始计算');
let learnCount = 0;//已看集数
let totalCount = 0;//总集数

//自动查找已看集数
let curPage = document.getElementsByClassName('cur-page');
if (curPage.length !== 1) {
  console.warn('cur-page=' + curPage.length);
} else {
  let innerText = curPage[0].innerText;// 如"(10/12)"
  innerText = innerText.substring(1, innerText.length - 1);
  let splits = innerText.split('/');
  learnCount = parseInt(splits[0]);
  totalCount = parseInt(splits[1]);
}

if (!learnCount || !totalCount) {
  console.warn('自动获取数量失败');
}

let total = 0;//总时长
let remain = 0;//未看时长
let videos = document.querySelectorAll('.list-box .duration, .video-episode-card__info-duration');//兼容两种页面
if (!totalCount) totalCount = videos.length;

videos.forEach((item, i) => {
    //$('.list-box').find('.duration').each((i, item) => {//jq
    let span = item.innerHTML.split(':');
    let time;
    if (span.length === 3) {
      time = parseInt(span[0]) * 3600 + parseInt(span[1]) * 60 + parseInt(span[2]);
    } else {
      time = parseInt(span[0]) * 60 + parseInt(span[1]);
    }
    total += time;
    if (i + 1 > learnCount) remain += time;
  }
);
let parseTime = second => {
  return `${(Math.floor(second / 3600)).toFixed(0)}:${(Math.floor(second / 60 % 60)).toFixed(0)}:${second % 60}`;
};
console.log(`已看集数=${learnCount}/${totalCount}, 剩余时长:${parseTime(remain)} / ${parseTime(total)}, 进度:${(1 - (remain / total)).toFixed(2)}`);